<?php
/**
* Display the transactions in a table fashion
*
* @author       Fabrice Douteaud <admin@clearbudget.net>
* @package      snippets
* @access       public
*/

/***********************************************************************

  Copyright (C) 2008  Fabrice Douteaud (admin@clearbudget.net)

    This file is part of ClearBudget.

    ClearBudget is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClearBudget is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClearBudget.  If not, see <http://www.gnu.org/licenses/>.


************************************************************************/
//prevent direct access
if(!defined('ENGINEON')) die('Direct access forbidden');
// build the back link URL to enable to be back to this list from a sub page (i.e.: the break transaction page)
$backLink = urlencode($context->action.'&ajax=1&monthSelected='.$context->monthSelected.'&debit='.$context->debit.'&categorySelected='.$context->categorySelected.'&keyWord='.$context->keyWord);
?>
<a name="editTransactionManualy"></a>
<div id="editTransactionManualy" class="hidden"></div>
<h2><?php echo $keys->tableTitle_transactions; ?></h2>
<table class="tableReport">
<?php
  $alt = '';
  echo '<tr><th><u>'.$keys->text_payee.'</u></th><th><u>'.$keys->text_memo.'</u></th><th><u>'.$keys->text_amountLong.'</u></th><th><u>'.$keys->text_operationDate.'</u></th><th><u>'.$keys->text_categoryNameLong.'</u></th><th colspan="4">&nbsp;</th></tr>';
  $total = 0;
  foreach($results as $result) {
  if($result['debit'] == "1") {
    $amountStyle = 'color:red;';
    $amountSign = '-';
    $total -= $result['amount'];
    }
  else {
    $amountStyle = 'color:green;';
    $amountSign = '+';
    $total += $result['amount'];
    }
  if($result['importType'] == IMPORTMANUAL) $editable = true;
  else $editable = false;
  if($alt == "") $alt = 'class="alt"';
  else $alt = '';
  $commentFormId = 'transactionEditCommentForm-'.$result['id'];
  $categoryFormId = 'transactionEditCategoryForm-'.$result['id'];
  $commentLoadingImage = 'commentLoading-'.$result['id'];
  $categoryLoadingImage = 'catLoading-'.$result['id'];
  // presentation row
  echo '<tr class="hoverHighlight">';
  echo '<td '.$alt.' style="width:500px">'.$result['payee'].'</td>';
  echo '<td '.$alt.'  style="width:300px"><form id="'.$commentFormId.'" onSubmit="transactionEditForm(\''.$commentFormId.'\', \''.$commentLoadingImage.'\', '.$result['id'].'); return false">';
  echo '<input type="hidden" name="id" value="'.$result['id'].'">';
  echo '<input class="commentEntry" type="text" size="20" name="comment" value="'.$result['comments'].'">'; 
  echo '<input type="image" src="style/icons/disk.png" title="'.$keys->link_save.'">';
  echo '<span><img class="hidden" id="'.$commentLoadingImage.'" src="style/icons/icon_Loading.gif"></span></form></td>';
  echo '<td '.$alt.' style="width:90px;'.$amountStyle.'">'.class_currency::getCurrencySymbol().$amountSign.$report->formatNumber($result['amount']).'</td>';
  echo '<td '.$alt.' style="width:170px">'.utf8_encode(strftime('%A %d %B %y', strtotime($result['operationDate']))).'</td>';
  echo '<td '.$alt.' style="width:250px"><form id="'.$categoryFormId.'" onSubmit="transactionEditForm(\''.$categoryFormId.'\', \''.$categoryLoadingImage.'\', '.$result['id'].'); return false">';
  echo '<input type="hidden" name="id" value="'.$result['id'].'">';
  echo '<select name="category" class="categorySelector">';
  // list all possible categories
  foreach($categories as $id=>$name) {
    // show the uncategorized option only if item is already uncategorized
    if($id == 1 && $result['category'] != 1) continue;
    echo '<option value="'.$id.'"';
    if($result['category'] == $id) echo ' selected="selected"';
    echo '>'.$name.'</option>';
    }
  echo '</select> <input type="image" src="style/icons/disk.png" title="'.$keys->link_save.'">';
  echo '<span><img class="hidden" id="'.$categoryLoadingImage.'" src="style/icons/icon_Loading.gif"></span></form>';
  echo '</td>';
  
  // the edit button
  echo '<td '.$alt.'>';
  if($editable && $result['parentId'] == 0) { 
    echo '<a href="#addTransactionsManualy:editTransactionManualy&id='.$result['id'].'" title="'.$keys->linkText_taskEditLink.'">';
    echo '<img src="style/icons/table_edit.png"/>';
    echo '</a>';
    }
  else {
    echo '&nbsp;';//<img src="style/icons/delete.png" disabled/>';
    }
  echo '</td>';
  
  // the delete button
  if($result['parentId'] == 0) {
    echo '<td '.$alt.'><a href="javascript:deleteTransaction(\''.$result['id'].'\')" title="'.$keys->link_delete.'"><img id="buttonImg-'.$result['id'].'" src="style/icons/bin_empty.png"/></a></td>';
    }
  else {
    echo '<td '.$alt.'>&nbsp;</td>';//<img src="style/icons/delete.png"/></td>';
    }
  
  // the break transaction button
  echo '<td '.$alt.'><a href="#breakTransaction&backLink='.$backLink.'&id='.$result['id'].'" title="'.$keys->linkText_breakTransactionLink.'">';
  if($result['parentId'] > 0) echo '<img src="style/icons/coins.png"/>';
  else echo '<img src="style/icons/coins_add.png"/>';
  echo '</a></td>';
  
  // the add to schedule button
  echo '<td '.$alt.'><a href="#taskScheduler&backLink='.$backLink.'&id='.$result['id'].'" title="'.$keys->linkText_taskReminderLink.'">';
  echo '<img src="style/icons/clock_add.png"/>';
  echo '</a></td>';
  echo '</tr>';
  }
if($total > 0) {
  $amountStyle = 'color:green;';
  $amountSign = '+';
  }
else {
  $amountStyle = 'color:red; font-weight:bold;';
  $amountSign = '';
  }
?>
<thead>
  <tr>
    <th colspan="2">Transaction summary</th>
    <th style="<?php echo $amountStyle;?>"><?php echo class_currency::getCurrencySymbol().$amountSign.$report->formatNumber($total); ?></th>
    <th colspan="6"></th>
  </tr>
</thead>
<tfoot>
  <tr>
    <th colspan="2">Transaction summary</th>
    <th style="<?php echo $amountStyle;?>"><?php echo class_currency::getCurrencySymbol().$amountSign.$report->formatNumber($total); ?></th>
    <th colspan="6"></th>
  </tr>
</tfoot>
</table>